<?php

namespace app\admin\model\shop;

use think\Model;


class Coupon extends Model
{

    

    

    // 表名
    protected $name = 'shop_coupon';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'create_time_text',
        'is_sys_text',
        'status_text'
    ];
    

    
    public function getTypeList()
    {
        return ['user' => __('Type user'), 'order' => __('Type order'), 'normal' => __('Type normal')];
    }

    public function getIsSysList()
    {
        return ['0' => __('Is_sys 0'), '1' => __('Is_sys 1')];
    }

    public function getStatusList()
    {
        return ['doing' => __('Status doing'), 'stop' => __('Status stop')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsSysTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_sys']) ? $data['is_sys'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getIsSysList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setIsSysAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


    public function shop()
    {
        return $this->belongsTo('app\admin\model\Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
